﻿using System;
using System.IO;
using XBee;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TDSServerLib{
    /*
     * Class for a actual data received from a machine
     */ 
    class DataExcersice{
        //  Fitness Machine source
        public XBeeAddress64   Source;
        //  Data from sensors without processing
        public MemoryStream    DataFromSensors;

        public ExcersiceSerieInformation ExcersiceInformation;
    }

    public class TableData{
        public double   _time0;
        public double   _time1;
        public double   _time;
        public int      _rep;
        public double   _ampM;
        public double   _speedM;
        public int      _weight;
        public double   _powerM;
    }

    public class ExcersiceSerieInformation{
        
        public List<TableData> _seriesInfo;
        public TableData _seriesTotal;
        public double[] _amp;
        public double[] _speed;
        public double[] _acc;
        public double[] _power;
        public int weight;
        public int time;
        public int rep;
    }

    public enum FitnesExcersice : int
    {
        POLEA_ALTA = 0,
        CUADRICEPS = 1
    } 

    class Processing{
        public DataExcersice[] _DataBaseMachine;


        DataExcersice _DataBasePoleaAlta = new DataExcersice();
        DataExcersice _DataBaseCuadriceps = new DataExcersice();

        public void SetupDataBaseMachine(MachinesContainer _machines){
            int n_machines = _machines.Machines.Count;
            _DataBaseMachine = new DataExcersice[n_machines];

            for (int i = 0; i < n_machines; i++)
                _DataBaseMachine[i] = new DataExcersice();
            

            int _index = 0;

            foreach (DataExcersice _database in _DataBaseMachine){
                string _addr = _machines.Machines.ElementAt(_index).MAC;
                _database.Source = new XBeeAddress64(ulong.Parse(_addr, System.Globalization.NumberStyles.HexNumber));
                ResetData(_index);
                _index ++;
            }
        }


        public void SaveNewData(byte[] _data, int _index){
            _DataBaseMachine[_index].DataFromSensors.Write(_data, 0, _data.Length);
        }


        public void SaveNewData(byte[] _data, FitnesExcersice _machine ){
            switch (_machine){
                case FitnesExcersice.POLEA_ALTA:
                    _DataBasePoleaAlta.DataFromSensors.Write(_data, 0, _data.Length);
                    break;
                case FitnesExcersice.CUADRICEPS:
                    _DataBaseCuadriceps.DataFromSensors.Write(_data, 0, _data.Length);
                    break;
                default:
                    break;
            }
        }

        public void ResetData(int _index){
            _DataBaseMachine[_index].DataFromSensors = new MemoryStream();
            _DataBaseMachine[_index].ExcersiceInformation = new ExcersiceSerieInformation();
            
        }

        public void ResetData(FitnesExcersice _machine){
            switch (_machine){
                case FitnesExcersice.POLEA_ALTA:
                    _DataBasePoleaAlta.DataFromSensors = new MemoryStream();
                    _DataBasePoleaAlta.ExcersiceInformation = new ExcersiceSerieInformation();
                    break;
                case FitnesExcersice.CUADRICEPS:
                    _DataBaseCuadriceps.DataFromSensors = new MemoryStream();
                    _DataBaseCuadriceps.ExcersiceInformation = new ExcersiceSerieInformation();
                    break;
                default:
                    break;
            }
        }

        public void ProcessExcersice(int _index, int weight){
            _DataBaseMachine[_index].ExcersiceInformation.weight = weight;
            _DataBaseMachine[_index].ExcersiceInformation._amp = ConvertByteArrayToDouble(_DataBaseMachine[_index].DataFromSensors.ToArray());
            _DataBaseMachine[_index].ExcersiceInformation._speed = LowPassFilter(LowPassFilter(derivativeDouble(_DataBaseMachine[_index].ExcersiceInformation._amp)));
            // A = dV/dt
            _DataBaseMachine[_index].ExcersiceInformation._acc = LowPassFilter(LowPassFilter(derivativeDouble(_DataBaseMachine[_index].ExcersiceInformation._speed)));
            // F = m(a-g)
            _DataBaseMachine[_index].ExcersiceInformation._acc = OffsetArrayDouble(_DataBaseMachine[_index].ExcersiceInformation._acc, -980.0);
            _DataBaseMachine[_index].ExcersiceInformation._acc = ScaleArrayDouble(_DataBaseMachine[_index].ExcersiceInformation._acc, (double)weight);
            // P = FxV
            _DataBaseMachine[_index].ExcersiceInformation._acc = MultArrayDouble(_DataBaseMachine[_index].ExcersiceInformation._acc, _DataBaseMachine[_index].ExcersiceInformation._speed);

            _DataBaseMachine[_index].ExcersiceInformation._acc = DivideArrayDouble(_DataBaseMachine[_index].ExcersiceInformation._acc, 10000.0);

            _DataBaseMachine[_index].ExcersiceInformation._seriesInfo = new List<TableData>();
            GetInfoOfRep(_index);
        }

        public void ProcessExcersice(FitnesExcersice _machine, int weight){
            switch (_machine){
                case FitnesExcersice.POLEA_ALTA:
                    _DataBasePoleaAlta.ExcersiceInformation.weight = weight;
                    _DataBasePoleaAlta.ExcersiceInformation._amp = ConvertByteArrayToDouble(_DataBasePoleaAlta.DataFromSensors.ToArray());
                    _DataBasePoleaAlta.ExcersiceInformation._speed = LowPassFilter(LowPassFilter(derivativeDouble(_DataBasePoleaAlta.ExcersiceInformation._amp)));
                    // A = dV/dt
                    _DataBasePoleaAlta.ExcersiceInformation._acc = LowPassFilter(LowPassFilter(derivativeDouble(_DataBasePoleaAlta.ExcersiceInformation._speed)));
                    // F = m(a-g)
                    _DataBasePoleaAlta.ExcersiceInformation._acc = OffsetArrayDouble(_DataBasePoleaAlta.ExcersiceInformation._acc, -980.0);
                    _DataBasePoleaAlta.ExcersiceInformation._acc = ScaleArrayDouble(_DataBasePoleaAlta.ExcersiceInformation._acc, (double)weight);
                    // P = FxV
                    _DataBasePoleaAlta.ExcersiceInformation._acc = MultArrayDouble(_DataBasePoleaAlta.ExcersiceInformation._acc, _DataBasePoleaAlta.ExcersiceInformation._speed);

                    _DataBasePoleaAlta.ExcersiceInformation._acc = DivideArrayDouble(_DataBasePoleaAlta.ExcersiceInformation._acc, 10000.0);
       
                    _DataBasePoleaAlta.ExcersiceInformation._seriesInfo = new List<TableData>();
                    GetInfoOfRep(FitnesExcersice.POLEA_ALTA);
                    break;
                case FitnesExcersice.CUADRICEPS:
                    _DataBaseCuadriceps.ExcersiceInformation.weight = weight;
                    _DataBaseCuadriceps.ExcersiceInformation._amp = ConvertByteArrayToDouble(_DataBaseCuadriceps.DataFromSensors.ToArray());
                    _DataBaseCuadriceps.ExcersiceInformation._speed = LowPassFilter(LowPassFilter(derivativeDouble(_DataBaseCuadriceps.ExcersiceInformation._amp)));
                    // A = dV/dt
                    _DataBaseCuadriceps.ExcersiceInformation._acc = LowPassFilter(LowPassFilter(derivativeDouble(_DataBaseCuadriceps.ExcersiceInformation._speed)));
                    // F = m(a-g)
                    _DataBaseCuadriceps.ExcersiceInformation._acc = OffsetArrayDouble(_DataBaseCuadriceps.ExcersiceInformation._acc, -980.0);
                    _DataBaseCuadriceps.ExcersiceInformation._acc = ScaleArrayDouble(_DataBaseCuadriceps.ExcersiceInformation._acc, (double)weight);
                    // P = FxV
                    _DataBaseCuadriceps.ExcersiceInformation._acc = MultArrayDouble(_DataBaseCuadriceps.ExcersiceInformation._acc, _DataBaseCuadriceps.ExcersiceInformation._speed);

                    _DataBaseCuadriceps.ExcersiceInformation._acc = DivideArrayDouble(_DataBaseCuadriceps.ExcersiceInformation._acc, 10000.0);

                    _DataBaseCuadriceps.ExcersiceInformation._seriesInfo = new List<TableData>();
                    GetInfoOfRep(FitnesExcersice.CUADRICEPS);
                    break;
                default:
                    break;
            }
            
        }

        public ExcersiceSerieInformation GetDataExcersice(int _index){
            ExcersiceSerieInformation _dummy = new ExcersiceSerieInformation();
            return _DataBaseMachine[_index].ExcersiceInformation;
        }

        public ExcersiceSerieInformation GetDataExcersice(FitnesExcersice _machine){
            ExcersiceSerieInformation _dummy = new ExcersiceSerieInformation();
            switch (_machine){
                case FitnesExcersice.POLEA_ALTA:
                    return _DataBasePoleaAlta.ExcersiceInformation;
                case FitnesExcersice.CUADRICEPS:
                    return _DataBaseCuadriceps.ExcersiceInformation;
                default:
                    return _dummy;
            }
        }

        public void GetInfoOfRep(int _index){
            TableData _table;
            int _sampleOffset = 5;
            int _rangeOffset = 2;
            for (int i = 1; i < _DataBaseMachine[_index].ExcersiceInformation._speed.Length; i++)
            {
                try
                {
                    if ((_DataBaseMachine[_index].ExcersiceInformation._speed[i] < 0.0) && (_DataBaseMachine[_index].ExcersiceInformation._speed[i - 1] >= 0.0))
                    {
                        if (i >= 5)
                        {
                            if ((_DataBaseMachine[_index].ExcersiceInformation._amp[i - 5] > (_DataBaseMachine[_index].ExcersiceInformation._amp[i] + 5)) || ((_DataBaseMachine[_index].ExcersiceInformation._amp[i - 5] <= (_DataBaseMachine[_index].ExcersiceInformation._amp[i] - 5))))
                            {
                                if (_DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Count == 0)
                                {
                                    _table = new TableData();
                                    _table._time0 = 0;
                                    _table._time1 = ((double)i * 110) / 1000.0;
                                    _table._time = _table._time1 - _table._time0;
                                    _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Add(_table);
                                }
                                else
                                {
                                    _table = new TableData();
                                    _table._time0 = _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Last()._time1;
                                    _table._time1 = ((double)i * 110) / 1000.0;
                                    _table._time = _table._time1 - _table._time0;
                                    _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Add(_table);
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            foreach (TableData Table in _DataBaseMachine[_index].ExcersiceInformation._seriesInfo)
            {
                Table._weight = _DataBaseMachine[_index].ExcersiceInformation.weight;
                Table._ampM = meanOfArray(_DataBaseMachine[_index].ExcersiceInformation._amp, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                Table._speedM = meanOfArrayAbs(_DataBaseMachine[_index].ExcersiceInformation._speed, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                Table._powerM = meanOfArrayAbs(_DataBaseMachine[_index].ExcersiceInformation._acc, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
            }

            // GET THE MEAN OF SERIES
            _DataBaseMachine[_index].ExcersiceInformation._seriesTotal = new TableData();
            foreach (TableData Table in _DataBaseMachine[_index].ExcersiceInformation._seriesInfo)
            {
                _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._ampM += Table._ampM;
                _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._time += Table._time;
                _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._speedM += Table._speedM;
                _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._powerM += Table._powerM;
            }
            _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._ampM = _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._ampM / _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Count;
            _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._time = _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._time / _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Count;
            _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._speedM = _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._speedM / _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Count;
            _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._powerM = _DataBaseMachine[_index].ExcersiceInformation._seriesTotal._powerM / _DataBaseMachine[_index].ExcersiceInformation._seriesInfo.Count;
        }

        public void GetInfoOfRep(FitnesExcersice _machine){
            TableData _table;
            int _sampleOffset = 5;
            int _rangeOffset = 2;
            switch (_machine){
                case FitnesExcersice.POLEA_ALTA:
                    for (int i = 1; i < _DataBasePoleaAlta.ExcersiceInformation._speed.Length; i++){
                        try{
                            if ((_DataBasePoleaAlta.ExcersiceInformation._speed[i] < 0.0) && (_DataBasePoleaAlta.ExcersiceInformation._speed[i - 1] >= 0.0)){
                                if ((_DataBasePoleaAlta.ExcersiceInformation._amp[i - 5] > (_DataBasePoleaAlta.ExcersiceInformation._amp[i] + 5)) || ((_DataBasePoleaAlta.ExcersiceInformation._amp[i - 5] <= (_DataBasePoleaAlta.ExcersiceInformation._amp[i] - 5))))
                                {
                                    if (_DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Count == 0)
                                    {
                                        _table = new TableData();
                                        _table._time0 = 0;
                                        _table._time1 = ((double)i * 110) / 1000.0;
                                        _table._time = _table._time1 - _table._time0;
                                        _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Add(_table);
                                    }
                                    else
                                    {
                                        _table = new TableData();
                                        _table._time0 = _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Last()._time1;
                                        _table._time1 = ((double)i * 110) / 1000.0;
                                        _table._time = _table._time1 - _table._time0;
                                        _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Add(_table);
                                    }
                                }
                            }
                        }
                        catch{
                        }
                    }
                    foreach (TableData Table in _DataBasePoleaAlta.ExcersiceInformation._seriesInfo){
                        Table._weight = _DataBasePoleaAlta.ExcersiceInformation.weight;
                        Table._ampM = meanOfArray(_DataBasePoleaAlta.ExcersiceInformation._amp, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                        Table._speedM = meanOfArrayAbs(_DataBasePoleaAlta.ExcersiceInformation._speed, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                        Table._powerM = meanOfArrayAbs(_DataBasePoleaAlta.ExcersiceInformation._acc, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                    }

                    // GET THE MEAN OF SERIES
                    _DataBasePoleaAlta.ExcersiceInformation._seriesTotal = new TableData();
                    foreach (TableData Table in _DataBasePoleaAlta.ExcersiceInformation._seriesInfo){
                        _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._ampM += Table._ampM;
                        _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._time += Table._time;
                        _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._speedM += Table._speedM;
                        _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._powerM += Table._powerM;
                    }
                    _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._ampM = _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._ampM / _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Count;
                    _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._time = _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._time / _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Count;
                    _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._speedM = _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._speedM / _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Count;
                    _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._powerM = _DataBasePoleaAlta.ExcersiceInformation._seriesTotal._powerM / _DataBasePoleaAlta.ExcersiceInformation._seriesInfo.Count;

                    break;
                case FitnesExcersice.CUADRICEPS:
                    for (int i = 1; i < _DataBaseCuadriceps.ExcersiceInformation._speed.Length; i++)
                    {
                        try
                        {
                            if ((_DataBaseCuadriceps.ExcersiceInformation._speed[i] < 0.0) && (_DataBaseCuadriceps.ExcersiceInformation._speed[i - 1] >= 0.0))
                            {
                                if ((_DataBaseCuadriceps.ExcersiceInformation._amp[i - 5] > (_DataBaseCuadriceps.ExcersiceInformation._amp[i] + 5)) || ((_DataBaseCuadriceps.ExcersiceInformation._amp[i - 5] <= (_DataBaseCuadriceps.ExcersiceInformation._amp[i] - 5))))
                                {
                                    if (_DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Count == 0)
                                    {
                                        _table = new TableData();
                                        _table._time0 = 0;
                                        _table._time1 = ((double)i * 110) / 1000.0;
                                        _table._time = _table._time1 - _table._time0;
                                        _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Add(_table);
                                    }
                                    else
                                    {
                                        _table = new TableData();
                                        _table._time0 = _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Last()._time1;
                                        _table._time1 = ((double)i * 110) / 1000.0;
                                        _table._time = _table._time1 - _table._time0;
                                        _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Add(_table);
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                    foreach (TableData Table in _DataBaseCuadriceps.ExcersiceInformation._seriesInfo)
                    {
                        Table._weight = _DataBaseCuadriceps.ExcersiceInformation.weight;
                        Table._ampM = meanOfArray(_DataBaseCuadriceps.ExcersiceInformation._amp, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                        Table._speedM = meanOfArrayAbs(_DataBaseCuadriceps.ExcersiceInformation._speed, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                        Table._powerM = meanOfArrayAbs(_DataBaseCuadriceps.ExcersiceInformation._acc, (int)((Table._time0 * 1000.0) / 110), (int)((Table._time1 * 1000.0) / 110));
                    }

                    // GET THE MEAN OF SERIES
                    _DataBaseCuadriceps.ExcersiceInformation._seriesTotal = new TableData();
                    foreach (TableData Table in _DataBaseCuadriceps.ExcersiceInformation._seriesInfo)
                    {
                        _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._ampM += Table._ampM;
                        _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._time += Table._time;
                        _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._speedM += Table._speedM;
                        _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._powerM += Table._powerM;
                    }
                    _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._ampM = _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._ampM / _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Count;
                    _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._time = _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._time / _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Count;
                    _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._speedM = _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._speedM / _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Count;
                    _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._powerM = _DataBaseCuadriceps.ExcersiceInformation._seriesTotal._powerM / _DataBaseCuadriceps.ExcersiceInformation._seriesInfo.Count;

                    break;
                default:
                    break;
            }
        }

        // Math Operations
        //--------------------------------------------------------------

        //public bool checkItIsInRange (double _value, 
        public double[] derivativeDouble(double[] _data){
            double[] _result = new double[_data.Length];
            int h = 1;
            int h2 = 2 * h;
            for (int i = 0; i < _data.Length; i++){
                try{
                    if (i < _data.Length -1)
                        _result[i] = (_data[i + h] - _data[i]) / h;
                    else
                        _result[i] = 0.0;
                }
                catch{
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public double meanOfArray (double[] _data, int _index0, int _index1){
            double _value = 0.0;
            for (int i = _index0; i < _index1; i++){
                try{
                    _value += _data[i];
                }
                catch{
                }
            }

            return (_value / (double)(_index1 - _index0));
        }

        public double meanOfArrayAbs(double[] _data, int _index0, int _index1)
        {
            double _value = 0.0;
            for (int i = _index0; i < _index1; i++)
            {
                try
                {
                    _value += Math.Abs(_data[i]);
                }
                catch
                {
                }
            }

            return (_value / (double)(_index1 - _index0));
        }

        public double[] ConvertByteArrayToDouble(byte[] _data){
            double[] _dataDouble = new double[_data.Length];
            for (int x = 0; x < _data.Length; x++)
                _dataDouble[x] = (double)_data[x];
            return _dataDouble;
        }

        public double[] LowPassFilter(double[] _data){
            double[] _result = new double[_data.Length];

            for (int i = 0; i < _data.Length; i++){
                try{
                    if (i > 0)
                        _result[i] = _data[i] + _data[i - 1];
                    else
                        _result[i] = 0.0;
                }
                catch{
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public double[] LowPassFilterForRepresent(double[] _data)
        {
            double[] _result = new double[_data.Length];

            for (int i = 0; i < _data.Length; i++)
            {
                try
                {
                    if (i > 0)
                        _result[i] = (_data[i] + _data[i - 1])/2;
                    else
                        _result[i] = 0.0;
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public double[] ScaleArrayDouble(double[] _data, double _scale)
        {
            double[] _result = new double[_data.Length];

            for (int i = 0; i < _data.Length; i++)
            {
                try
                {
                    _result[i] = _data[i] * _scale;
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public double[] DivideArrayDouble(double[] _data, double _scale)
        {
            double[] _result = new double[_data.Length];

            for (int i = 0; i < _data.Length; i++)
            {
                try
                {
                    _result[i] = _data[i] / _scale;
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }


        public double[] OffsetArrayDouble(double[] _data, double _offset)
        {
            double[] _result = new double[_data.Length];

            for (int i = 0; i < _data.Length; i++)
            {
                try
                {
                    _result[i] = _data[i] + _offset;
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public double[] MultArrayDouble(double[] _data1, double[] _data2)
        {
            double[] _result = new double[_data1.Length];

            for (int i = 0; i < _data1.Length; i++)
            {
                try
                {
                    _result[i] = _data1[i] * _data2[i];
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }
    }
}
